package com.sanery.controller;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sanery.service.SiteService;
import com.sanery.service.impl.SiteServiceImpl;

@Controller
public class AddController {
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(HttpSession session) {
		if (session.getAttribute("userid") == null) {
			return "redirect:/login";
		}
		return "/add";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(HttpSession session, String s_title, String s_cgy, String s_link) {
		Integer u_id = (Integer) session.getAttribute("userid");
		if (u_id == null) {
			return "redirect:/login";
		}
		try {
			SiteService service = new SiteServiceImpl();
			service.addSite(s_title, s_cgy, s_link, u_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/list";
	}
}
